﻿using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.Photo;
using SouthernLand.Common.Untilities;

namespace SouthernLand.WebMaster.Photos
{
    public partial class Albums : Page
    {
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Action"] == "Edit")
            {
                CateID = Convert.ToInt32(Request.QueryString["CateID"]);
                btAlbumAdd.CommandArgument = "Edit";
                if (!IsPostBack)
                    Load_AlbumByCateID(CateID);
            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridViewItem.Rows)
            {
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int ID = Convert.ToInt32(((CheckBox)row.FindControl("chkSelect")).ToolTip.Trim());
                    PhotoCategory.Delete(ID);
                }
            }
            DataBind();
        }

        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            if (!string.IsNullOrEmpty(FilePhoto.FileName))
            {
                strFileName = Common.Common.GetFileName() + Path.GetExtension(FilePhoto.FileName);
                string strExtension = Path.GetExtension(FilePhoto.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" ||
                    strExtension == ".PNG")
                {
                    string strFilePath = Request.PhysicalApplicationPath + "//Gallery//" + txtAppKey.Text.Trim() + "//" +
                                         strFileName;

                    FileProcess.UpLoadImage(FilePhoto, strFilePath, (int)PhotoSize.Medium);
                }
                else
                {
                    strFileName = "";
                }
            }
            return strFileName;
        }

        private void Load_AlbumByCateID(int cateId)
        {
            PhotoCategory obj = PhotoCategory.GetCateByCateID(cateId);
            if (obj != null)
            {
                txtAppKey.Text = obj.AppKey;
                txtAppKey.Enabled = false;
                txtAlbumName.Text = obj.CateName;
                txtAlbumName_en.Text = obj.CateName_en;
                txtAlbumDesc.Text = obj.CateDesc;
                txtCateOrder.Text = obj.CateOrder.ToString();
                chkIsPrivate.Checked = CheckAlbumPrivate(obj.CateID);
                ViewState["image"] = obj.ImageUrl;
            }
        }

        protected bool CheckAlbumPrivate(int cateId)
        {
            PhotoCategory obj = PhotoCategory.GetCateByCateID(cateId);
            return obj.ParentCateID == (int)EnumCategory.AlbumsPrivate;
        }

        protected void AlbumsAdd_Click(object sender, EventArgs e)
        {
            if (btAlbumAdd.CommandArgument == "Edit")
            {
                AlbumEdit();
                btAlbumAdd.CommandArgument = "Add";
            }
            else
                AlbumAdd();
            Form_Clear();
        }

        private void AlbumAdd()
        {
            var obj = new PhotoCategory();
            obj.ImageUrl = FilePhoto_Upload();
            obj.AppKey = txtAppKey.Text.Trim();
            obj.CateName = txtAlbumName.Text.Trim();
            obj.CateDesc = txtAlbumDesc.Text;
            obj.CateName_en = txtAlbumName_en.Text;
            obj.CateDesc_en = "";
            obj.CateOrder = txtCateOrder.Text == string.Empty ? 0 : Convert.ToInt32(txtCateOrder.Text);
            obj.ParentCateID =
                Convert.ToInt32(chkIsPrivate.Checked ? EnumCategory.AlbumsPrivate : EnumCategory.AlbumsPublic);
            PhotoCategory.Insert(obj);
        }

        private void AlbumEdit()
        {
            string strFileName = string.Empty;
            strFileName = !string.IsNullOrEmpty(FilePhoto.FileName) ? FilePhoto_Upload() : ViewState["image"].ToString();
            var obj = new PhotoCategory();
            obj.CateID = CateID;
            obj.ImageUrl = strFileName;
            obj.AppKey = txtAppKey.Text.Trim();
            obj.CateName = txtAlbumName.Text.Trim();
            obj.CateDesc = txtAlbumDesc.Text;
            obj.CateName_en = txtAlbumName_en.Text;
            ;
            obj.CateDesc_en = "";
            obj.CateOrder = txtCateOrder.Text == "" ? 0 : Convert.ToInt32(txtCateOrder.Text);
            obj.ParentCateID =
                Convert.ToInt32(chkIsPrivate.Checked ? EnumCategory.AlbumsPrivate : EnumCategory.AlbumsPublic);
            PhotoCategory.Update(obj);
            txtAppKey.Enabled = true;
        }

        private void Form_Clear()
        {
            GridViewItem.DataBind();
            txtAppKey.Text = "";
            txtAlbumDesc.Text = "";
            txtAlbumName.Text = "";
            txtCateOrder.Text = "0";
            chkIsPrivate.Checked = false;
            lblMsg.Visible = false;
        }

        protected bool CreateFolderAlbum(string appKey)
        {
            if (Directory.Exists(Request.PhysicalApplicationPath + "//Gallery//" + appKey))
                return false;
            else
            {
                Directory.CreateDirectory(Request.PhysicalApplicationPath + "//Gallery//" + appKey);
                Directory.CreateDirectory(Request.PhysicalApplicationPath + "//Gallery//" + appKey + "//Small");
                Directory.CreateDirectory(Request.PhysicalApplicationPath + "//Gallery//" + appKey + "//Large");
                return true;
            }
        }

        protected void GridViewItem_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var btnDelete = e.Row.Cells[3].Controls[0] as ImageButton;
                btnDelete.OnClientClick =
                    "if (confirm('Nếu bạn chọn OK thì tất cả hình ảnh nằm trong Album này sẽ bị xóa?') == false) return false;";
                btnDelete.ToolTip = "Delete this post";
            }
        }
    }
}